package com.syxd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syxd.common.constant.Constant;
import com.syxd.common.entity.WebResult;
import com.syxd.common.exception.DIYException;
import com.syxd.common.exception.ErrorCode;
import com.syxd.common.service.impl.CrudServiceImpl;
import com.syxd.common.utils.BeanHelper;
import com.syxd.dao.SysConfigDao;
import com.syxd.dao.SysJiangCreditContactDao;
import com.syxd.dao.SysJiangCreditDao;
import com.syxd.dto.SysJiangCreditContactDto;
import com.syxd.dto.SysJiangCreditDto;
import com.syxd.entity.SysJiangCreditContactEntity;
import com.syxd.entity.SysJiangCreditEntity;
import com.syxd.entity.SysSettingEntity;
import com.syxd.service.SysConfigService;
import com.syxd.service.SysJiangCreditContactService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.sql.Array;
import java.util.*;

@Service
@Transactional
public class SysJiangCreditContactServiceImpl extends CrudServiceImpl<SysJiangCreditContactDao, SysJiangCreditContactEntity, SysJiangCreditContactDto> implements SysJiangCreditContactService {
    @Autowired
    SysJiangCreditContactDao sysJiangCreditContactDao;
    @Autowired
    SysJiangCreditDao sysJiangCreditDao;
    @Autowired
    SysUserServiceImpl sysUserService;
    @Autowired
    SysConfigDao sysConfigDao;
    @Override
    public WebResult delJiangCreditContact(Long id, HttpServletRequest request) {

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("is_delete",Constant.FDELETE);
        SysJiangCreditEntity sysJiangCreditEntity = sysJiangCreditDao.selectOne(queryWrapper);
        SysSettingEntity sysSettingEntity = sysConfigDao.selectById(1);
        if (sysJiangCreditEntity.getLastupdateTime()==null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sysJiangCreditEntity.getCreateTime());
            calendar.add(calendar.DATE,sysSettingEntity.getSettingDate());
            Date time = calendar.getTime();
            if (System.currentTimeMillis()<time.getTime()){
                throw new DIYException(ErrorCode.TIME_ERROR, String.valueOf(sysSettingEntity.getSettingDate()), "");
            }
        }else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sysJiangCreditEntity.getLastupdateTime());
            calendar.add(calendar.DATE,sysSettingEntity.getSettingDate());
            Date time = calendar.getTime();
            if (System.currentTimeMillis()<time.getTime()){
                throw new DIYException(ErrorCode.TIME_ERROR, String.valueOf(sysSettingEntity.getSettingDate()), "");
            }

        }

        SysJiangCreditContactEntity sysJiangCreditContactEntity = BeanHelper.copyProperties(sysJiangCreditEntity, SysJiangCreditContactEntity.class);
        sysJiangCreditContactEntity.setSourceDataId(Math.toIntExact(id));
        sysJiangCreditContactEntity.setCreateTime(new Date());
        sysJiangCreditContactEntity.setOperationType(Constant.operationType.delete.getValue());
        sysJiangCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.wait.getValue());
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request,"userId").getData());
        sysJiangCreditContactEntity.setApplicantId(userId);
        sysJiangCreditContactEntity.setUpdateTime(null);
        sysJiangCreditContactDao.insert(sysJiangCreditContactEntity);
        return new WebResult();
    }

    @Override
    public WebResult update(SysJiangCreditDto dto, HttpServletRequest request) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",dto.getId());
        queryWrapper.eq("is_delete",Constant.FDELETE);
        SysJiangCreditEntity sysJiangCreditEntity = sysJiangCreditDao.selectOne(queryWrapper);
        SysSettingEntity sysSettingEntity = sysConfigDao.selectById(1);
        if (sysJiangCreditEntity.getLastupdateTime()==null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sysJiangCreditEntity.getCreateTime());
            calendar.add(calendar.DATE,sysSettingEntity.getSettingDate());
            Date time = calendar.getTime();
            if (System.currentTimeMillis()<time.getTime()){
                throw new DIYException(ErrorCode.TIME_ERROR, String.valueOf(sysSettingEntity.getSettingDate()), "");
            }
        }else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sysJiangCreditEntity.getLastupdateTime());
            calendar.add(calendar.DATE,sysSettingEntity.getSettingDate());
            Date time = calendar.getTime();
            if (System.currentTimeMillis()<time.getTime()){
                throw new DIYException(ErrorCode.TIME_ERROR, String.valueOf(sysSettingEntity.getSettingDate()), "");
            }

        }
        SysJiangCreditContactEntity sysJiangCreditContactEntity = BeanHelper.copyProperties(dto, SysJiangCreditContactEntity.class);

        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request,"userId").getData());
        sysJiangCreditContactEntity.setApplicantId(userId);
        sysJiangCreditContactEntity.setSourceDataId(dto.getId().intValue());
        sysJiangCreditContactEntity.setOperationType(Constant.operationType.update.getValue());
        sysJiangCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.wait.getValue());
        sysJiangCreditContactEntity.setCreateTime(new Date());
        sysJiangCreditContactEntity.setUpdateTime(null);
        sysJiangCreditContactDao.insert(sysJiangCreditContactEntity);
        return new WebResult();
    }

    @Override
    public WebResult put(SysJiangCreditDto dto, HttpServletRequest request) {
        //新增业务判断是否存在,将户主身份证Id设为唯一
        if (dto.getHouseholderIdcard() != null) {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("householder_idcard", dto.getHouseholderIdcard());
            SysJiangCreditEntity sysJiangCreditEntity = sysJiangCreditDao.selectOne(queryWrapper);
            if (sysJiangCreditEntity != null) {
                return new WebResult().error(ErrorCode.household_already_exists);
            }
        }
        SysJiangCreditContactEntity sysJiangCreditContactEntity = BeanHelper.copyProperties(dto, SysJiangCreditContactEntity.class);
        Integer userId = Integer.valueOf((String) sysUserService.getInfoFromToken(request,"userId").getData());
        sysJiangCreditContactEntity.setApplicantId(userId);
        sysJiangCreditContactEntity.setOperationType(Constant.operationType.add.getValue());
        sysJiangCreditContactEntity.setProcessingStatus(Constant.ProcessingStatus.wait.getValue());
        sysJiangCreditContactEntity.setCreateTime(new Date());
        sysJiangCreditContactEntity.setUpdateTime(null);
        sysJiangCreditContactDao.insert(sysJiangCreditContactEntity);
        return new WebResult();
    }


    @Override
    public QueryWrapper<SysJiangCreditContactEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<SysJiangCreditContactEntity> wrapper = new QueryWrapper<>();
        String processingStatus = (String) params.get("processingStatus");
        String branchName = (String) params.get("branchName");
        String householderIdcard = (String) params.get("householderIdcard");
        String householderPhone = (String) params.get("householderPhone");
        String spouseName = (String) params.get("spouseName");
        String spouseIdcard = (String) params.get("spouseIdcard");
        String spousePhone = (String) params.get("spousePhone");
        String householderName = (String) params.get("householderName");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");
        if (StringUtils.isNotBlank(endDate)) {
            endDate = endDate + " 23:59:59";
        }

        if(StringUtils.isNotBlank(processingStatus)){
            wrapper.eq( "processing_status", processingStatus);
        }else {
            wrapper.eq("processing_status", 1);
        }
        wrapper.eq(StringUtils.isNotBlank(branchName), "branch_name", branchName);
        wrapper.eq(StringUtils.isNotBlank(householderIdcard), "householder_idcard", householderIdcard);
        wrapper.eq(StringUtils.isNotBlank(householderPhone), "householder_phone", householderPhone);
        wrapper.eq(StringUtils.isNotBlank(spouseName), "spouse_name", spouseName);
        wrapper.eq(StringUtils.isNotBlank(spouseIdcard), "spouse_idcard", spouseIdcard);
        wrapper.eq(StringUtils.isNotBlank(spousePhone), "spouse_phone", spousePhone);
        wrapper.eq(StringUtils.isNotBlank(householderName), "householder_name", householderName);
        wrapper.ge(StringUtils.isNotBlank(startDate),"activity_start_date",startDate);
        wrapper.le(StringUtils.isNotBlank(endDate),"activity_start_date",endDate);
        return wrapper;
    }

    @Override
    public WebResult importJiangList(Map map) {
        List<Object> arrayList= (List<Object>) map.get("list");
        for (Object o : arrayList) {

            System.out.println("o = " + o);
        }

    return  null;
    }

}
